package module

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2010-11-21
 * Time: 18:44:42
 * To change this template use File | Settings | File Templates.
 */
class CMBMainPageModule extends BankMainPageModule {

  CMBMainPageModule(def buffer) {
    super(buffer)
  }

  public void defineUi() {
    ui.Selector(uid: "citySelector", clocator: [name: 'ddlCity'], respond: ['onchange'])
    ui.UrlLink(uid: "page1", locator: "//html/body/form/table[2]/tbody/tr/td/table[3]/tbody/tr/td/table/tbody/tr[32]/td/font/a")
    (2..10).each {
      ui.UrlLink(uid: "page${it}", locator: "//html/body/form/table[2]/tbody/tr/td/table[3]/tbody/tr/td/table/tbody/tr[32]/td/font/a[${it}]")
    }
  }

  public getAllCities() {
    getSelectOptions('citySelector')[1..-1]
  }

  public selectCity(city) {
    select('citySelector', city)
    waitForPageToLoad 100000
    waitForElementPresent('citySelector', 200000)
    def source = getSource()
    buffer.produce "<root>" + source + "</root>"
    //def bankInfos = new CMBBankInfoResolver().resolveData("<root>"+source+"</root>")
    (1..10).findAll {
      if (isElementPresent("page${it}")) {
        click "page${it}"
        waitForPageToLoad 100000
        waitForElementPresent('citySelector', 200000)
        source = getSource()
        buffer.produce "<root>" + source + "</root>"
        //bankInfos.addAll new CMBBankInfoResolver().resolveData("<root>"+source+"</root>")
        true
      } else {
        false
      }
    }
  }

  public def getSource() {
    return this.getHtmlSource().replaceAll("<br>", "").replaceAll("<img.*\">", "").replaceAll("<meta.*\">", "").replaceAll("<link.*\">", "").replaceAll("<input.*>", "").replaceAll("window.document.location.href.*", "").replaceAll("&nbsp;", "").replaceAll("&", "").replaceAll("<=57", "")
  }
}
